package model;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class Connect {
	String SID;
	String user;
	String pass;
	public Connect(){
		this.SID="orcl";
		this.user="sys";
		this.pass="qwerty";
	}
	public Connect(String SID,String user, String pass){
		this.SID=SID;
		this.user=user;
		this.pass=pass;
	}
	public void setSID(String SID){
		this.SID=SID;
	}
	public void setUser(String user){
		this.user=user;
	}
	public void setPassword(String pass){
		this.pass= pass;
	}
	public Connection create_connect(){
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			Properties props = new Properties();
			props.put("user",this.user);
			props.put("password", this.pass);
			props.put("internal_logon","sysdba");
			return DriverManager
				.getConnection("jdbc:oracle:thin:@localhost:1521:"+this.SID, props);	
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
		
	}
	public Connection create_connect(String role){
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			Properties props = new Properties();
			props.put("user",this.user);
			props.put("password", this.pass);
			props.put("internal_logon",role);
			return DriverManager
				.getConnection("jdbc:oracle:thin:@localhost:1521:"+this.SID, props);	
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	public Connection simple_connect(){
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			return DriverManager
				.getConnection("jdbc:oracle:thin:@localhost:1521:"+this.SID, this.user,this.pass);	
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	public Account checkSession(HttpServletRequest request, HttpServletResponse response) throws IOException{
	
		HttpSession session = request.getSession(true);
		Account acc = (Account)session.getAttribute("acc");
		if(acc == null)
			response.sendRedirect("JSP/Login.jsp");
		return acc;
		
	}
	public String getdbSession(HttpServletRequest request) throws IOException{
		HttpSession session = request.getSession(true);
		return (String)session.getAttribute("SID");
	}
		
	public String passdbSession(HttpServletRequest request) throws IOException{
		HttpSession session = request.getSession(true);
		return (String)session.getAttribute("passSYS");
	}
}
